package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import bean.NewsBean;
import utils.RequestQueueUtils;
import zhang.automobileservice.R;


/**
 * Created by Administrator on 2016/7/31.
 */
public class NewsAdapter extends BaseAdapter {
    private List<NewsBean> comboList;
    private Context mContext;
    private LayoutInflater inflater;

    public NewsAdapter(List<NewsBean> comboList, Context mContext) {
        this.comboList = comboList;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    public void addData(List<NewsBean> list) {
        for (int i = 0; i < list.size(); i++) {
            comboList.add(list.get(i));
        }
    }

    @Override
    public int getCount() {
        return comboList.size();
    }

    @Override
    public Object getItem(int position) {
        return comboList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.newslist_item, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        String url = comboList.get(position).getTop_image();
        RequestQueueUtils.exectueNetWorkImageView(mContext, url, viewHolder.iv_listimage);

        viewHolder.tv_newstitle.setText(comboList.get(position).getTitle());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        viewHolder.tv_time.setText(simpleDateFormat.format(new Date(1000*Long.parseLong(comboList.get(position).getEdit_time()))));
        viewHolder.tv_count.setText(comboList.get(position).getReply_count());
        return convertView;
    }


    public static class ViewHolder {
        public View rootView;
        public NetworkImageView iv_listimage;
        public TextView tv_newstitle;
        public TextView tv_time;
        public TextView tv_count;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_listimage = (NetworkImageView) rootView.findViewById(R.id.iv_listimage);
            this.tv_newstitle = (TextView) rootView.findViewById(R.id.tv_newstitle);
            this.tv_time = (TextView) rootView.findViewById(R.id.tv_time);
            this.tv_count = (TextView) rootView.findViewById(R.id.tv_count);
        }

    }
}
